package com.bawei.a2010douyin.ui.login;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import okhttp3.MediaType;
import okhttp3.RequestBody;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bawei.a2010douyin.R;
import com.bawei.a2010douyin.base.mvp.BaseMvpActivity;
import com.bawei.a2010douyin.base.utils.ARouterPath;
import com.bawei.a2010douyin.base.utils.ConstantUtils;
import com.bawei.a2010douyin.db.dao.DBUtils;
import com.bawei.a2010douyin.entity.LoginEntity;
import com.bawei.a2010douyin.entity.User;
import com.bawei.a2010douyin.im.IMCallBack;
import com.bawei.a2010douyin.im.IMUtils;
import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;

import java.util.HashMap;

@Route(path = ARouterPath.LOGIN_AROUTER_PATH)
public class LoginActivity extends BaseMvpActivity<LoginContract.ILoginView,LoginPresenter> implements LoginContract.ILoginView {

    private EditText etUsername;
    private EditText etPassword;



    @Override
    public int bindLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        etUsername = (EditText) findViewById(R.id.et_username);
        etPassword = (EditText) findViewById(R.id.et_password);
    }

    @Override
    public LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void initData() {

    }
    //登陆成功
    @Override
    public void showLogin(LoginEntity loginEntity) {
        //环信登陆
        IMUtils.getInstance().login(loginEntity.getUsername(), loginEntity.getPassword(), new IMCallBack<String, String>() {
            @Override
            public void onOk(String s) {
                Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                SPUtils.getInstance().put("token",loginEntity.getToken());//将token存在sp中，再判断是否登陆过，不需要跳转到登陆页面
                //将用户信息存储到数据库中
                User user = new User();
                user.setUsername(loginEntity.getUsername());
                user.setMoney(1000f);
                DBUtils.getInstance().insert(user);


                finish();

            }

            @Override
            public void onError(String s) {
                Toast.makeText(LoginActivity.this, "环次呢登陆失败"+s, Toast.LENGTH_SHORT).show();
            }
        });



    }
    //登陆失败
    @Override
    public void onError() {
        super.onError();
        Toast.makeText(this, "登陆失败", Toast.LENGTH_SHORT).show();
    }
    //点击登陆
    /**
     * {
     *   "password": "string",
     *   "username": "string"
     * }
     */
    public void login(View view) {
        //提交请求体：json数据
        HashMap<String, String> map = new HashMap<>();
        map.put("username",etUsername.getText().toString());
        map.put("password",etPassword.getText().toString());
        String json = new Gson().toJson(map);
        //json数据--》RequestBody
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), json);
        mPresenter.login(requestBody);
    }
    //去注册
    public void register(View view) {
        ARouter.getInstance()
                .build(ARouterPath.REGISTER_AROUTER_PATH)
                .navigation(this, ConstantUtils.REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode ==  ConstantUtils.REQUEST_CODE && resultCode == ConstantUtils.RESULT_CODE && data != null){
            Bundle bundle = data.getExtras();
            etUsername.setText(bundle.getString("username"));
            etPassword.setText(bundle.getString("password"));
        }
    }

}